package com.shiliao.app.weight;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.shiliao.app.R;
import com.yimei.framework.util.CommonUtil;


/**
 * Created by huihuili on 2017/6/19.
 */

public class ActionBarView extends RelativeLayout {

    private RelativeLayout mBarView;
    private TextView mTitle;
    private TextView mOther;
    private ImageView mBack;

    public ActionBarView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    public ActionBarView(Context context) {
        super(context);
        initView(context);
    }

    private void initView(Context context) {
        mBarView = (RelativeLayout) View.inflate(context, R.layout.actionbar, null);
        addView(mBarView);
        mTitle = (TextView) mBarView.findViewById(R.id.action_title);
        mOther = (TextView) mBarView.findViewById(R.id.action_other);
        mBack = (ImageView) mBarView.findViewById(R.id.actionbar_back);

    }

    public void setTitles(String titleId) {
        if (mTitle != null) {
            mTitle.setVisibility(View.VISIBLE);
            mTitle.setText(titleId);
        }
    }

    public void setRightTitle(String titleId) {
        if (mOther != null) {
            mOther.setVisibility(View.VISIBLE);
            mOther.setText(titleId);
        }
    }

    public void setBack(int back) {
        if (mBack != null) {
            mBack.setVisibility(View.VISIBLE);
            mBack.setImageResource(back);
        }
    }

    public void setBackClick(final Activity aty) {
        mBack.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                aty.finish();
            }
        });
    }

    public void setBackToActivity(final Activity activity, final Class toActivity) {
        mBack.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                CommonUtil.toActivity(activity, new Intent(activity, toActivity));
            }
        });
    }

    public TextView getTitleView() {
        return mTitle;
    }

    public ImageView getBackView() {
        return mBack;
    }

    public TextView getOtherView() {
        return mOther;
    }
}

